﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevTools.Romanizer.Components
{
    public class StringConverter
    {
        #region singleton
        private static StringConverter _instance;

        public static StringConverter Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new StringConverter();
                return _instance;
            }
        }

        private StringConverter()
        {
        }
        #endregion

        public string Convert(string source, Casing casing)
        {
            return Convert(source, casing, Padding.None);
        }

        public string Convert(string source, Casing casing, Padding padding)
        {
            var builder = new StringBuilder();

            for (int i = 0; i < source.Length; i++)
            {
                var value = CharConverter.Instance[source[i]];

                if (casing == Casing.Upper)
                    value = value.ToUpper();
                else if (casing == Casing.Pascal)
                    value = value.ToProperNoun();
                else if (casing == Casing.ProperNoun)
                    value = (i == 0) ? value.ToProperNoun() : value;
                else if (casing == Casing.Camel)
                    value = (i == 0) ? value : value.ToProperNoun();

                builder.Append(value);


                if (padding == Padding.Space && i < source.Length - 1)
                    builder.Append(" ");
            }

            return builder.ToString();
        }
    }
}